#ifndef __MESSAGEBUILDER_H__
#define __MESSAGEBUILDER_H__

#include "loggerdef.h"

class Logger;

class MessageBuilder
{
    friend class Logger;

public:
    explicit MessageBuilder(Logger *logger);
    virtual ~MessageBuilder();

public:
    MessageBuilder& operator<<(bool msg);
    MessageBuilder& operator<<(uint8_t msg);
    MessageBuilder& operator<<(int8_t msg);
    MessageBuilder& operator<<(uint16_t msg);
    MessageBuilder& operator<<(int16_t msg);
    MessageBuilder& operator<<(uint32_t msg);
    MessageBuilder& operator<<(int32_t msg);
    MessageBuilder& operator<<(uint64_t msg);
    MessageBuilder& operator<<(int64_t msg);
    MessageBuilder& operator<<(float msg);
    MessageBuilder& operator<<(double msg);
    MessageBuilder& operator<<(char msg);
    MessageBuilder& operator<<(const char *msg);
    MessageBuilder& operator<<(void *msg);

    MessageBuilder& operator<<(const std::string &msg);
    MessageBuilder& operator<<(const std::string &&msg);

    //printf
    MessageBuilder& Print(const char *fmt, ...);
    MessageBuilder& PrintLn(const char *fmt, ...);

    //config
    MessageBuilder &NoSpace();
    MessageBuilder &Space();
    MessageBuilder &NoNewline();
    MessageBuilder &Newline();
    MessageBuilder &NoFormat(); //无格式输出
    MessageBuilder &Format(); //有格式输出

    MessageBuilder &AsRaw(); //写入什么就输出什么
    
    //强制写入，无视logger开关, 特殊场景有用
    MessageBuilder &Force();

    MessageBuilder &AsTag(const std::string &tag);

    MessageBuilder &ConfigLevel(LoggerLevel level);

    MessageBuilder &Precision(int precision);

private:
    void AppendSpace();
    std::string GetFormatMessage();
    char* GetTimeString();
    std::string ConvertLevelToShortString(LoggerLevel level);
    void ConvertToShortFileName(std::string &fileName);

    std::string MiniFormat(char const *const format, ...);

private:
    std::string m_messages;

    //call params
    Logger* m_logger = nullptr;
    LoggerLevel m_level = LoggerLevel::Debug;
    std::string m_fileName; //source file name
    int m_fileLine; //souce file line 
    std::string m_funcName;  //source func name
    std::string m_tag; //[本类可定制]
    
    //config [本类可定制]
    bool m_formatEnable = true;
    //default from logger [本类可定制]
    bool m_autoSpacing = true;
    bool m_autoNewLine = true;
    bool m_forceWrite = false;

    //浮点数精度 [本类可定制]
    //默认7为精度
    uint8_t m_precision = 7;
};

#endif // __MESSAGEBUILDER_H__
